package com.crm.service.impl;

import cn.hutool.core.lang.func.Func;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.CountDto.ClientsDto;
import com.crm.entity.Clue;
import com.crm.entity.Pool;
import com.crm.mapper.BusinessMapper;
import com.crm.dto.BusinessDto;
import com.crm.entity.Business;
import com.crm.result.PageResult;
import com.crm.result.PageResultTow;
import com.crm.result.Result;
import com.crm.service.BusinessService;
import com.crm.vo.BusinessVo;
import com.crm.vo.BusinessVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.netty.util.internal.StringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@AllArgsConstructor
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    private final BusinessMapper businessMapper;

    /**
     * 新增商机
     *
     * @param business
     */
    @Override
    public void insert(Business business) {
        business.setStatus(1);
        business.setBusiness_type(1);
        business.setContract_type(0);
        log.info("新增商机:{}", business);
        businessMapper.insert(business);
    }

    /**
     * 分页查询
     *
     * @param businessDto
     * @return
     */
    @Override
    public PageResultTow PageBusinessList(BusinessDto businessDto) {
        LocalDateTime beginTime = businessDto.getBeginCreateTime() != null
                ? businessDto.getBeginCreateTime().toInstant()
                .atZone(java.time.ZoneId.systemDefault())
                .toLocalDateTime()
                : null;

        LocalDateTime endTime = businessDto.getEndCreateTime() != null
                ? businessDto.getEndCreateTime().toInstant()
                .atZone(java.time.ZoneId.systemDefault())
                .toLocalDateTime()
                : null;
        Page<Business> page = new Page<>(businessDto.getPageNum(), businessDto.getPageSize());
        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(businessDto.getPhone()), Business::getPhone, businessDto.getPhone());
        wrapper.like(StringUtils.isNotEmpty(businessDto.getName()), Business::getName, businessDto.getName());
        wrapper.eq(StringUtils.isNotEmpty(JSONUtil.toJsonStr(businessDto.getId())), Business::getId, businessDto.getId());
        wrapper.like(StringUtils.isNotEmpty(businessDto.getOwner()), Business::getOwner, businessDto.getOwner());
        wrapper.like(StringUtils.isNotEmpty(JSONUtil.toJsonStr(businessDto.getStatus())), Business::getStatus, businessDto.getStatus());
        wrapper.ge(businessDto.getBeginCreateTime() != null, Business::getCreateTime, businessDto.getBeginCreateTime())
                .le(businessDto.getEndCreateTime() != null, Business::getCreateTime, businessDto.getEndCreateTime());
//        当business_type等于1的时候显示，否则不显示
        wrapper.eq(Business::getBusiness_type, 1);
        wrapper.eq(Business::getContract_type, 0);
        businessMapper.selectPage(page, wrapper);
        List<Business> records = page.getRecords();
        return new PageResultTow(200, "查询成功", null, page.getTotal(), records);


    }

    /**
     * 根据id查询  跟进商机信息
     *
     * @param id
     * @return
     */
    @Override
    public Business selectByBusinessId(Integer id) {
        return businessMapper.selectById(id);
    }

    /**
     * 统计客户,分页查询
     *
     * @param
     * @return
     */
//    @Override
//    public PageResultTow countClients(ClientsDto clientsDto) {
//        Page<Business> page = new Page<>(clientsDto.getPageNum(), clientsDto.getPageSize());
//        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
////        wrapper.eq(StringUtils.isNotEmpty(JSONUtil.toJsonStr(clientsDto.getId())),Business::getId, clientsDto.getId());
////        wrapper.eq(StringUtils.isNotEmpty(clientsDto.getName()), Business::getName,  clientsDto.getName());
//        return null;
//    }
    @Override
    public void putBusiness(Integer businessId, Integer reason) {
        businessMapper.updateBusiness(businessId, reason);
        log.info("将商机id为{}的商机转成合同id为{}的商机", businessId, reason);
    }


}
